package com.yuoj.yuojcodesandbox.utils;

import cn.hutool.core.date.StopWatch;
import com.yuoj.yuojcodesandbox.model.ExecuteMessage;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * @author 刘渠好
 * @since 2025-03-17 22:43
 * 处理进程信息工具类
 */
@Slf4j
public class ProcessUtils {

    public static ExecuteMessage runProcessAndGetMessage(Process process, String opName) {
        ExecuteMessage executeMessage = new ExecuteMessage ();
        try {
            //计时
            StopWatch stopWatch = new StopWatch ();
            stopWatch.start ();
            int exitValue = process.waitFor ();
            executeMessage.setExitValue (exitValue);
            //正常退出
            if (exitValue == 0) {
                log.info ("{}成功!", opName);
                //分批获得进程的正常输出
                BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (process.getInputStream ()));
                StringBuilder compileOutputStringBuilder = new StringBuilder ();
                //进行读取
                String compileOutputLine;
                while ((compileOutputLine = bufferedReader.readLine ()) != null) {
                    compileOutputStringBuilder.append (compileOutputLine);
                }
                executeMessage.setMessage (compileOutputStringBuilder.toString ());
                log.info ("正常输出:{}", compileOutputStringBuilder);
            } else {
                //异常退出
                log.info ("编译失败，错误码：{}", exitValue);

                //分批获取异常的错误输出
                BufferedReader errorBufferedReader = new BufferedReader (new InputStreamReader (process.getErrorStream ()));
                StringBuilder errorOutputStringBuilder = new StringBuilder ();
                //进行读取
                String errorOutputLine;
                while ((errorOutputLine = errorBufferedReader.readLine ()) != null) {
                    errorOutputStringBuilder.append (errorOutputLine);
                }
                executeMessage.setErrorMessage (errorOutputStringBuilder.toString ());
                log.info ("异常输出:{}", errorOutputStringBuilder);
            }

            stopWatch.stop ();
            executeMessage.setTime (stopWatch.getLastTaskTimeMillis ());

        } catch (Exception e) {
            throw new RuntimeException (e);

        }

        return executeMessage;
    }


    public static ExecuteMessage runInteractProcessAndGetMessage(Process process, String args,String opName) {

        OutputStream outputStream = process.getOutputStream ();
        try {
//            outputStream.write ((args + "\n").getBytes ());
            outputStream.write (args.getBytes ());
            //将数值打在控制台，回车
            outputStream.flush ();
            //关闭流操作
            outputStream.close ();
            //复用
            return runProcessAndGetMessage (process, opName);
        } catch (IOException e) {
            throw new RuntimeException (e);
        } finally {
            try {
                outputStream.close ();
            } catch (IOException e) {
                log.error ("关闭输入流失败!");
            }
        }

    }

}
